package org.duang.push;

import java.util.List;

import org.duang.common.dto.ReturnDto;
import org.duang.common.enums.IEnums;
import org.duang.kit.ThreadPoolKit;
import org.duang.kit.ToolsKit;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * 推送工具类
 * @author laotang
 */
public class PushKit {
	
	private static PushKit pushKit;
	private String message;
	private String[] pushToIds = null;
	
	public static PushKit duang() {
		synchronized (PushKit.class) {
			if(ToolsKit.isEmpty(pushKit)){
				pushKit = new PushKit();
			}
		}
		return pushKit;
	}
	
	public PushKit body(String message) {
		this.message = message;
		return this;
	}
	
	public PushKit to(String... pushToIds) {
		String[] tmpIds = (String[]) pushToIds;
		this.pushToIds = tmpIds;
		return this;
	}
	
	public PushKit body(ReturnDto<Object> dto) {
		this.message = ToolsKit.toJsonString(dto);
		return this;
	}

	public void push() {		
		try {
			ThreadPoolKit.execute(new PushThread(pushToIds, new TextWebSocketFrame(true, IEnums.IENUMS_SUCCESS_CODE, message)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
